package com.ibm.servlets;



import com.ibm.entity.User;
import com.ibm.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserAddServlet")
public class UserAddServlet  extends HttpServlet {


    private UserService userService=new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException{
        request.setCharacterEncoding("utf-8");

        UserService userService=new UserService();

        String userId = request.getParameter("userId");
        String userName=request.getParameter("userName");
        String pwd=request.getParameter("pwd");
        String score=request.getParameter("score");
        System.out.println();
        User user=new User(Integer.parseInt(userId),userName,pwd,Integer.parseInt(score));

       boolean b=userService.saveUser(user);

        //3.返回到列表页面（？？？）
       String tips = b?"<label style='color:green'用户添加成功！</label>"
            :"<label style='color:red'>用户添加失败！</label>";
        //当再次回到admin_manager_list.jsp页面时，依然需要管理员列表和角色列表
        List<User> userList = userService.listUser();


        request.setAttribute("tips", tips);//提示信息
        request.setAttribute("userList",userList);//用户列表

        request.getRequestDispatcher("member-show.jsp").forward(request,response);
    }
}
